<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="1200px" @close="close" :close-on-click-modal="false" v-dialogDrag>
    <vab-query-form>
      <el-form inline ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据号" prop="docCode">
          <el-input v-model="form.docCode" autocomplete="off" maxlength="50" style="width: 150px"></el-input>
        </el-form-item>
        <el-form-item label="单据日期" prop="docDate">
          <el-date-picker v-model="form.docDate" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="客户名称" prop="clientName">
          <el-input v-model="form.clientName" autocomplete="off" maxlength="50" style="width: 150px"></el-input>
        </el-form-item>
        <el-form-item label="销售人员" prop="salesman">
          <el-input v-model="form.salesman" autocomplete="off" maxlength="50" style="width: 150px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备注" prop="remark">
          <el-input placeholder="请输入内容" maxlength="200" v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
    </vab-query-form>
    <vab-query-form>
      <el-button icon="el-icon-plus" type="primary" @click="handleAdd">新增</el-button>
    </vab-query-form>
    <el-table border v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" show-summary max-height="500">
      <el-table-column align="center" min-width="50" label="操作">
        <template #default="{ row, $index }">
          <el-button icon="el-icon-delete" type="danger" size="mini" circle @click="handleDelete(row, $index)"></el-button>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="150" prop="roleCode" label="产品编码">
        <template #default="{ row }">
          <el-input v-model="row.productCode" readonly>
            <template slot="append"><el-button icon="el-icon-search"></el-button></template>
          </el-input>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="roleName" label="产品名称">
        <template #default="{ row }">
          <el-input v-model="row.productName" readonly></el-input>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="roleName" label="规格">
        <template #default="{ row }">
          <el-input v-model="row.spec" readonly></el-input>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="roleName" label="型号">
        <template #default="{ row }">
          <el-input v-model="row.modelName" readonly></el-input>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="roleName" label="单位">
        <template #default="{ row }">
          <el-input v-model="row.unitName" readonly></el-input>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="createdTime" label="单价">
        <template #default="{ row }">
          <el-input v-model="row.price"></el-input>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="createdBy" label="数量">
        <template #default="{ row }">
          <el-input v-model="row.number"></el-input>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="createdBy" label="金额">
        <template #default="{ row }">
          <el-input v-model="row.amount" readonly></el-input>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="200" prop="createdBy" label="备注">
        <template #default="{ row }">
          <el-input v-model="row.remark"></el-input>
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doAdd, doEdit } from '@/api/sales/order'

  export default {
    name: 'SalesOrderEdit',
    data() {
      return {
        list: [],
        listLoading: false,
        elementLoadingText: '正在加载...',
        form: {
          docCode: undefined,
          docDate: undefined,
          clientName: undefined,
          salesman: undefined,
          remark: undefined,
        },
        rules: {
          docDate: [{ required: true, trigger: ['blur', 'change'], message: '请选择单据日期' }],
          clientName: [{ required: true, trigger: ['blur', 'change'], message: '请选择客户' }],
          salesman: [{ required: true, trigger: ['blur', 'change'], message: '请输入销售人员' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      handleAdd() {
        this.list.push({})
      },
      handleDelete(row, index) {
        console.log(row, index)
        this.list.splice(1, index)
      },
      showEdit(row) {
        if (!row) {
          this.title = '新增'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.roleId) {
              //   await doEdit(this.form).then((res) => this.callbackFun(res))
              // } else {
              //   await doAdd(this.form).then((res) => this.callbackFun(res))
            }
          } else {
            return false
          }
        })
      },
      callbackFun(res) {
        if (res.success) {
          this.$baseMessage(res.msg, 'success')
          this.$emit('fetch-data')
          this.close()
        } else {
          this.$baseMessage(res.msg, 'error')
        }
      },
    },
  }
</script>
